package com.yhny.lykj.yuhannongye.net;

import com.longsichao.lscframe.ctrl.CacheHelper;

import java.math.BigDecimal;

/**
 * Created by HelloKitty on 2016/4/21.
 */
public class CacheHelpers {
    /***
     * 获取网络缓存大小
     * @return
     */
    public static String getCacheSize(){

        long size = CacheHelper.getDiskCacheSize()+ CacheHelper.getDiskCacheFileSize();
        if (size==0){
            return "0B";
        }
        return CacheHelper.formatCacheSize(size);
    }

    /**
     * 清空缓存
     */
    public static Boolean clearCache(){

        CacheHelper.clearDiskCacheFile();
        Boolean isCache = CacheHelper.clearDiskCache();
        return isCache;
    }
    /**
     * 格式化单位
     *
     * @param size
     * @return
     */
    public static String getFormatSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
//            return size + "Byte";
            return "0K";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
                + "TB";
    }
}
